<template>
  <div class="annotationWrapper">
    <el-form
      :inline="true"
      ref="headForm"
      :model="headFormData"
      class="search-form"
    >
      <el-form-item label="文件名称:" class="form-input">
        <el-input v-model="headFormData.file_name" clearable></el-input>
      </el-form-item>
      <el-form-item label="本体模型:" class="form-input">
        <el-select
          v-model="headFormData.ontology_model_id"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in options"
            :key="item.ontologyAllId"
            :label="item.ontologyAllName"
            :value="item.ontologyAllId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="onSubmit(1)"
          >查询</el-button
        >
      </el-form-item>
    </el-form>

    <div class="server-table">
      <div class="table-title">
        <span class="table-title-text">标注数据</span>
        <div>
          <el-button type="primary" size="small" @click="openDialog"
            >导入图谱</el-button
          >
        </div>
      </div>
      <el-table
        :data="tableList"
        @selection-change="handleSelectionChange"
        v-loading="tableLoading"
        class="km-table"
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          type="index"
          label="全选"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="fileName"
          label="文件名"
          align="center"
          show-overflow-tooltip
          min-width="300"
        ></el-table-column>
        <el-table-column
          prop="ontologyAllName"
          label="本体模型"
          align="center"
          width="150"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="entityNum"
          label="已标注实体数量"
          align="center"
          width="150"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="propertyNum"
          label="已标注属性数量"
          align="center"
          width="150"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="relationNum"
          label="已标注关系数量"
          align="center"
          show-overflow-tooltip
          width="150"
        >
        </el-table-column>
        <el-table-column
          prop="updateUserId"
          label="更新人"
          align="center"
          width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="updateTime"
          label="更新时间"
          align="center"
          width="150"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click.native.prevent="linkToCheckPage(scope.row.id,scope.row)"
              >查看
            </el-link>
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click.native.prevent="linkToEditPage(scope.row)"
              >编辑</el-link
            >
            <el-link
              :underline="false"
              type="primary"
              class="ontology-edit"
              @click.native.prevent="deleteTaggedData(scope.row.id)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="km-pagination"
        background
        :small="true"
        layout="total, prev, pager, next,sizes, jumper"
        :total="total"
        :hide-on-single-page="!total"
        :page-size.sync="headFormData.page_size"
        :pager-count="7"
        :page-sizes="[10, 20, 30]"
        :current-page="headFormData.page_index"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>

    <el-dialog title="导入图谱" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="dialogForm" ref="dialogForm" :rules="dialogRules">
        <el-form-item label="图谱名称:" prop="atlas" class="dialog-inline">
          <el-select v-model="dialogForm.atlas" placeholder="请选择导入的图谱">
            <el-option
              v-for="item in dialogOptions"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="submitDialogForm"
          :loading="submitDialogFormLoading"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "index",
  components: {},
  data: () => {
    return {
      headFormData: {
        file_name: "", // 文件名称
        ontology_model_id: "", // 本体模型
        page_index: 1, // 页码
        page_size: 10, // 每页条数
      },
      options: [],
      tableList: [],
      tableLoading: false,
      tableSelection: [],
      total: 0,
      dialogFormVisible: false,
      dialogForm: {
        atlas: "",
      },
      dialogOptions: [],
      dialogRules: {
        atlas: [
          {
            required: true,
            message: "请选择图谱",
            trigger: "blur",
          },
        ],
      },
      submitDialogFormLoading: false,
    };
  },
  computed: {},
  created() {
    this.getTaggedData();
    this.getOptions();
    this.getGraphName();
  },
  methods: {
    // 导入图谱按钮触发
    openDialog() {
      if (!this.tableSelection.length) {
        this.$message.warning("请先选择标注数据");
        return;
      }
      this.dialogFormVisible = true;
    },

    handleSelectionChange(e) {
      this.tableSelection = e.map((e) => e.id);
    },

    async getGraphName() {
      let res = await this.$api.knowledgeGeneration.chooseGraph();
      if (res.data.code !== 200) {
        this.$message.warning(res.data.message);
        return;
      }
      this.dialogOptions = res.data.data.map((item) => {
        const { graphId: id, graphName: label } = item;
        return {
          id,
          label,
        };
      });
    },

    // 弹窗确定
    submitDialogForm() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false;
          const loading = this.$message({
            showClose: true,
            dangerouslyUseHTMLString: true,
            duration: 0,
            customClass: "el-message-loading",
            message: `<div class="el-loading-spinner">
                        <svg viewBox="25 25 50 50" class="circular">
                          <circle cx="50" cy="50" r="20" fill="none" class="path">
                          </circle>
                        </svg>
                      </div>
                      <span class="el-message-loading__text">
                        导入中
                      </span>`,
          });
          this.$api.knowledgeGeneration
            .importAnnationData({
              graph_id: this.dialogForm.atlas,
              annation_rocord_ids: this.tableSelection,
            })
            .then((res) => {
              const { code, message, data } = res.data;
              if (code !== 200) {
                this.$message.warning(message);
                return;
              }
              this.$message.success(message);
              if (data.targetUrl == "entityLink") {
                setTimeout(
                  () =>
                    this.$router.push("/knowledgeGeneration/entityLink/index"),
                  1000
                );
              }
            })
            .catch((err) => {
              console.log(err);
              this.$message.warning(err.data.message);
            })
            .finally(() => {
              loading.close();
            });
        } else {
          return false;
        }
      });
    },

    // 修改当前页码
    handleCurrentChange(page) {
      this.headFormData.page_index = page;
      this.getTaggedData();
    },
    // 修改每页条数
    handleSizeChange() {
      this.getTaggedData(this.filterData);
    },

    // 获取标注数据
    async getTaggedData() {
      this.tableLoading = true;
      const { file_name, ontology_model_id, page_index, page_size } =
        this.headFormData;
      let params = {
        file_name,
        ontology_model_id,
        page_index,
        page_size,
      };
      let res = await this.$api.knowledgeGeneration.annotationDataList(params);
      this.tableLoading = false;
      if (res.data.code !== 200) {
        this.$message.warning(res.data.message);
        return;
      }
      this.tableList = res.data.data.list;
      this.total = res.data.data.total;
    },

    // 获取本体模型下拉框数据
    async getOptions() {
      let res = await this.$api.knowledgeGeneration.getOntologyModel();
      if (res.data.code !== 200) {
        this.$message.warning(res.data.message);
        return;
      }
      this.options = res.data.data;
    },

    onSubmit(page_index) {
      this.headFormData.page_index = page_index
        ? page_index
        : this.headFormData.page_index;
      this.getTaggedData();
    },

    // 跳转查看页
    linkToCheckPage(id,item) {
      this.$router.push({
        path: `/knowledgeGeneration/annotation/detail/${id}`,
        query: {
            file_id: item.fileId,
          },
        }
      );
    },

    // 跳转编辑(标注)页
    linkToEditPage(item) {
      let fileType = "";
      if (item.fileName) {
        fileType = item.fileName.split(".").pop().toLowerCase();
      }
      if (["png", "jpg", "jpeg"].includes(fileType)) {
        this.$router.push({
          path: "/knowledgeGeneration/annotation/annotationPic",
          query: {
            ontology_all_id: item.ontologyAllId,
            file_id: item.fileId,
            annotation_data_record_id: item.id,
          },
        });
      } else {
        this.$router.push({
          path: "/knowledgeGeneration/annotation/annotation",
          query: {
            ontology_all_id: item.ontologyAllId,
            file_id: item.fileId,
            annotation_data_record_id: item.id,
          },
        });
      }
    },

    // 删除
    deleteTaggedData(id) {
      console.log("id", id);
      this.$confirm(
        "是否确认删除所选标注数据？删除后数据不可恢复，请谨慎操作。",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          // 删除接口
          await this.deleteExtractData(id);
          this.getTaggedData();
        })
        .catch(() => {
          this.$message.info("已取消清空");
        });
    },
    async deleteExtractData(id) {
      let res = await this.$api.knowledgeGeneration.deleteAnnotationRecord({
        recordId: id,
      });
      if (res.data.code !== 200) {
        this.$message.warning(res.data.message);
        return;
      }
      this.$message.success("删除成功!");
    },
  },
};
</script>
<style lang="scss" scoped>
.annotationWrapper {
  .search-form {
    background: #ffffff;
    border-radius: 4px;
    padding: 16px 32px;
    .el-form-item {
      margin-bottom: 0;
    }

    .form-input {
      margin-right: 32px;
      .el-input,
      .el-select {
        ::v-deep .el-input__inner {
          width: 240px;
          height: 32px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #e0e0e0;
        }
      }
    }
  }

  .server-table {
    margin-top: 16px;
    padding: 16px 32px 64px;
    background: #ffffff;
    border-radius: 4px;

    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      &-text {
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
      }

      margin-bottom: 10px;
    }

    .ontology-edit {
      padding: 0 10px;
      & + .ontology-edit {
        border-left: 1px solid #dddddd;
      }
    }
  }

  .dialog-inline {
    /deep/ .el-form-item__content {
      width: 300px;
    }

    button.el-button {
      background-color: $--color-primary !important;
      color: #fff !important;
      line-height: 1.2;
    }
  }
}
</style>
<style lang="scss">
.el-message.el-message-loading {
  .el-message__content {
    .el-loading-spinner {
      margin-top: -17px;
      .circular {
        width: 36px;
        height: 36px;
      }
      text-align: left;
    }
    .el-message-loading__text {
      margin-left: 40px;
    }
  }
}
</style>
